import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        Solution s = new Solution();
        printMatrix(s.generateMatrix(4));
    }

    static void printMatrix(int[][] ans){
        for(int[] row: ans){
            System.out.println(Arrays.toString(row));
        }
    }
}


class Solution {
    public int[][] generateMatrix(int n) {
        int cur = 1;
        int[][] ans = new int[n][n];
        int i = 0, j = 0;
        while(cur <= n * n){
            ans[i][j] = cur++;
            if(j + 1 < n && ans[i][j + 1] == 0 && (i - 1 < 0 || ans[i - 1][j] != 0)){
                j++;
                continue;
            }
            if(i + 1 < n && ans[i + 1][j] == 0){
                i++;
                continue;
            }
            if(j - 1 >= 0 && ans[i][j - 1] == 0){
                j--;
                continue;
            }
            if(i - 1 >= 0 && ans[i - 1][j] == 0){
                i--;
            }
        }
        return ans;
    }
}